<!-- src/views/auth-redirect.vue -->
<template>
    <div class="loading-text">正在登录，请稍候...</div>
  </template>
  
  <script>
  import api from "../../utils/request";
  export default {
   // auth-redirect.vue
mounted() {

  const hash = window.location.hash.substr(1);
  const [path, query] = hash.split('?');
  const queryParams = new URLSearchParams(query);
  const token = queryParams.get('token');
  if (token) {
    localStorage.setItem('token', token);
    // ✅ 正确设置请求头
    if (this.$api.service) {
      this.$api.service.defaults.headers.common['Authorization'] = `Bearer ${token}`;
    }

    // ✅ 立即获取用户信息（关键）
    this.$api.getUserInfo()
      .then(res => {
        if (res.status_code === 1) {
          // ✅ 更新全局状态
          this.$globalData.userInfo = res.data;
          this.$router.replace('/index');
        } else {
          this.$router.replace('/login');
        }
      })
      .catch(() => {
        this.$router.replace('/login');
      });
    this.$router.replace('/index');
  }
}
  };
  </script>
  
  <style>
  .loading-text {
    text-align: center;
    margin-top: 100px;
    font-size: 18px;
  }
  </style>